//
//  YKClientDataDetailV2Controller.m
//  YunKang
//
//  Created by leoking870 on 2017/11/30.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientDataDetailV2Controller.h"
#import "YKClientDataModel.h"
#import "YKClientDataDetail.h"
#import "YKClientDataReadOnlyView.h"
#import "YKOwnerShipFromModel.h"
#import "YKDatTypeModel.h"
#import "YKCustomLevelModel.h"
#import "YKClientDataBasicInfoView.h"
#import "YKClientDataVisitInfoView.h"
#import "YKClientDataBasicInfoView.h"
#import "YKClientDataVisitInfoView.h"


@interface YKClientDataDetailV2Controller ()
@property(nonatomic, strong) YKClientDataDetail *mYKClientDataDetail;
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UIButton *basicButton;
@property(nonatomic, strong) UIButton *visitButton;
@property(nonatomic, strong) YKClientDataBasicInfoView *basicInfoView;
@property(nonatomic, strong) YKClientDataVisitInfoView *visitInfoView;
@end

@implementation YKClientDataDetailV2Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"查看客户详情";
    self.view.backgroundColor = [UIColor whiteColor];

    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
    }];

    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView.mas_width);
    }];

    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"customerDataManager/customerDataManagerDetail.htm" parameters:@{@"id": self.mYKClientDataModel.ID} success:^(id responseObject) {
        [self tf_showStateView:kTFViewStateNone];
        [CommonDataManager sharedInstance].medQuaList = [NSArray yy_modelArrayWithClass:[YKMedQuaModel class] json:responseObject[@"medQuaList"]];
        [CommonDataManager sharedInstance].settlemenTypeList = [NSArray yy_modelArrayWithClass:[YKSettlemenTypeModel class] json:responseObject[@"settlemenTypeList"]];
        [CommonDataManager sharedInstance].customerTypeList = [NSArray yy_modelArrayWithClass:[YKTypeModel class] json:responseObject[@"customerTypeList"]];
        [CommonDataManager sharedInstance].ownerShipFormList = [NSArray yy_modelArrayWithClass:[YKOwnerShipFromModel class] json:responseObject[@"ownerShipFormList"]];
        [CommonDataManager sharedInstance].datTypeList = [NSArray yy_modelArrayWithClass:[YKDatTypeModel class] json:responseObject[@"datTypeList"]];
        [CommonDataManager sharedInstance].customerLevelList = [NSArray yy_modelArrayWithClass:[YKCustomLevelModel class] json:responseObject[@"customerLevelList"]];
        self.mYKClientDataDetail = [YKClientDataDetail yy_modelWithJSON:responseObject[@"customerDataManagerMap"]];
        [self updateViews];
    }          failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}

- (void)updateViews {
    YKClientDataReadOnlyView *headerView = [[YKClientDataReadOnlyView alloc] initWithClientModel:self.mYKClientDataDetail];
    [self.contentView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.contentView);
    }];

    self.basicButton = [[UIButton alloc] init];
    [self.contentView addSubview:self.basicButton];
    [self.basicButton setTitle:@"基本信息" forState:UIControlStateNormal];
    [self.basicButton setBackgroundImage:TFSTYLE(navigationBarBackgroudImage) forState:UIControlStateSelected];
    [self.basicButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateNormal];
    [self.basicButton addTarget:self action:@selector(onBasicButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    self.visitButton = [[UIButton alloc] init];
    [self.contentView addSubview:self.visitButton];
    [self.visitButton setTitle:@"运营信息" forState:UIControlStateNormal];
    [self.visitButton setBackgroundImage:TFSTYLE(navigationBarBackgroudImage) forState:UIControlStateSelected];
    [self.visitButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateNormal];
    [self.visitButton addTarget:self action:@selector(onVisitButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    self.basicButton.selected = YES;
    self.visitButton.selected = NO;
    [@[self.basicButton, self.visitButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.mas_width).multipliedBy(.5f);
        make.height.mas_equalTo(40);
        make.top.equalTo(headerView.mas_bottom);
    }];
    [@[self.basicButton, self.visitButton] mas_alignAlongAxis:MASAxisTypeHorizontal fixedSpace:@0 leadSpace:0 tailSpace:0];

    self.basicInfoView = [[YKClientDataBasicInfoView alloc] initWithClientModel:self.mYKClientDataDetail];
    [self.contentView addSubview:self.basicInfoView];
    [self.basicInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView.mas_leading);
        make.trailing.equalTo(self.contentView.mas_trailing);
        make.top.equalTo(self.basicButton.mas_bottom);
        make.bottom.lessThanOrEqualTo(self.contentView.mas_bottom);
    }];

    self.visitInfoView = [[YKClientDataVisitInfoView alloc] initWithClientModel:self.mYKClientDataDetail];
    [self.contentView addSubview:self.visitInfoView];
    [self.visitInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView.mas_leading);
        make.trailing.equalTo(self.contentView.mas_trailing);
        make.top.equalTo(self.basicButton.mas_bottom);
        make.bottom.lessThanOrEqualTo(self.contentView.mas_bottom);
    }];
    self.visitInfoView.hidden = YES;

    UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onEditItemTapped:)];
    self.navigationItem.rightBarButtonItem = editItem;
}

- (void)onEditItemTapped:(UIBarButtonItem *)sender {
    if ([sender.title isEqualToString:@"编辑"]) {
        sender.title = @"完成";
        self.visitInfoView.editable = YES;
        self.basicInfoView.editable = YES;
    } else {
        sender.title = @"编辑";
        self.visitInfoView.editable = NO;
        self.basicInfoView.editable = NO;
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        [dictionary addEntriesFromDictionary:self.visitInfoView.values];
        [dictionary addEntriesFromDictionary:self.basicInfoView.values];
        dictionary[@"id"] = self.mYKClientDataDetail.ID;
        dictionary[@"c_name"] = self.mYKClientDataDetail.C_NAME;
        dictionary[@"address"] = self.mYKClientDataDetail.ADDRESS?:@"";
        dictionary[@"receiving_detailaddress"] = self.mYKClientDataDetail.RECEIVING_DETAILADDRESS;

        NSString *consignee_phone = dictionary[@"consignee_phone"];
        if ([consignee_phone tf_isPhotoNumber]) {
            [TFHTTPMANAER POST:@"customerDataManager/edtCustomerDataManager.htm" parameters:dictionary HUD:@"修改中..." success:^(id responseObject) {
                [self showHint:@"修改成功"];
            }          failure:^(id responseObject, NSError *error) {
                [self showHint:@"修改失败"];
            }];
        } else {
            [self showHint:@"请输入正确的手机号码"];
        }
    }
}

- (void)onVisitButtonTapped:(id)onVisitButtonTapped {
    self.basicButton.selected = NO;
    self.visitButton.selected = YES;
    self.visitInfoView.hidden = NO;
    self.basicInfoView.hidden = YES;
}

- (void)onBasicButtonTapped:(id)onBasicButtonTapped {
    self.basicButton.selected = YES;
    self.visitButton.selected = NO;
    self.visitInfoView.hidden = YES;
    self.basicInfoView.hidden = NO;
}

@end
